import { IContext, IEntityLocalDataService, IParam } from '@/core';
import { IAuth_info } from '../interface/i-auth-info';
import keys from '../auth-info-keys';
import { Auth_memberDTOHelp } from '@/api/auth-member/dto-help/auth-member-helps';
import { Auth_granted_systemDTOHelp } from '@/api/auth-granted-system/dto-help/auth-granted-system-helps';

/**
 * Auth_infoDTODTO辅助类
 * 基于template/src/api/{{appEntities}}/data-service/{{appEntities}}-help.ts.hbs生成
 *
 * @export
 * @class Auth_infoDTOHelp
 */
export class Auth_infoDTOHelp {
	/**
	 * 获取实体数据服务
	 *
	 * @static
	 * @param {IParam} context
	 * @return {*}  {Promise<IEntityLocalDataService<IAuth_info>>}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async getService(
		context: IParam
	): Promise<IEntityLocalDataService<IAuth_info>> {
		return App.getDataService('Auth_info', context);
	}

	/**
	 * 获取是否缓存
	 *
	 * @memberof Auth_infoDTOHelp
	 */
	public static getEnableCache() {
		return false;
	}

	/**
	 * DTO转化成数据对象
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} source dto对象
	 * @return {*}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async toDataObj(context: IContext, source: IParam) {
		const _data: any = {};
		//  用户标识
		_data.id = source.id;
		//  用户显示名
		_data.display_name = source.display_name;
		//  username/uid
		_data.username = source.username;
		//  DC
		_data.dc = source.dc;
		//  组织机构标识
		_data.organization_id = source.organization_id;
		//  电子邮箱
		_data.mail = source.mail;
		//  移动电话
		_data.mobile = source.mobile;
		//  头像
		_data.avatar = source.avatar;
		//  隶属于
		_data.member_of = source.member_of;
		//  member_of
		if (Auth_memberDTOHelp.getEnableCache()) {
			if (source && source.member_of && source.member_of.length > 0) {
				await Auth_memberDTOHelp.setCacheDataArray(context, source.member_of);
			}
			_data.member_of = source.member_of;
		} else {
			_data.member_of = source.member_of;
		}
		//  授予
		_data.grants = source.grants;
		//  grants
		if (Auth_granted_systemDTOHelp.getEnableCache()) {
			if (source && source.grants && source.grants.length > 0) {
				await Auth_granted_systemDTOHelp.setCacheDataArray(
					context,
					source.grants
				);
			}
			_data.grants = source.grants;
		} else {
			_data.grants = source.grants;
		}
		//  人员编号
		_data.employee_number = source.employee_number;
		//  类型
		_data.employee_type = source.employee_type;
		//  职位
		_data.title = source.title;
		//  联系电话
		_data.telephone_number = source.telephone_number;
		//  证件号
		_data.identification_number = source.identification_number;
		//  地址
		_data.postal_address = source.postal_address;
		//  处理预置字段（以srf开头）
		if (source && Object.keys(source).length > 0) {
			Object.keys(source).forEach((key: string) => {
				if (key.startsWith('srf')) {
					_data[key] = source[key];
				}
			});
		}
		return _data;
	}

	/**
	 * 转化数据（DTO转化成数据对象）
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据对象
	 * @return {*}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async toDataObjArray(context: IContext, data: any[]) {
		const _data: any[] = [];
		if (data && Array.isArray(data) && data.length > 0) {
			for (let i = 0; i < data.length; i++) {
				const tempdata = await this.toDataObj(context, data[i]);
				_data.push(tempdata);
			}
		}
		return _data;
	}

	/**
	 * 数据对象转化成DTO
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} source 数据对象
	 * @return {*}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async toDto(context: IContext, source: IParam) {
		const _data: any = {};
		const excludes: string[] = ['member_of', 'grants'];
		//  用户标识
		if (source.hasOwnProperty('id')) {
			_data.id = source.id;
		}
		//  用户显示名
		if (source.hasOwnProperty('display_name')) {
			_data.display_name = source.display_name;
		}
		//  username/uid
		if (source.hasOwnProperty('username')) {
			_data.username = source.username;
		}
		//  DC
		if (source.hasOwnProperty('dc')) {
			_data.dc = source.dc;
		}
		//  组织机构标识
		if (source.hasOwnProperty('organization_id')) {
			_data.organization_id = source.organization_id;
		}
		//  电子邮箱
		if (source.hasOwnProperty('mail')) {
			_data.mail = source.mail;
		}
		//  移动电话
		if (source.hasOwnProperty('mobile')) {
			_data.mobile = source.mobile;
		}
		//  头像
		if (source.hasOwnProperty('avatar')) {
			_data.avatar = source.avatar;
		}
		//  隶属于
		if (source.hasOwnProperty('member_of')) {
			_data.member_of = source.member_of;
		}
		//  member_of
		if (source.hasOwnProperty('member_of')) {
			_data.member_of = source.member_of;
		} else if (Auth_memberDTOHelp.getEnableCache()) {
			_data.member_of = await Auth_memberDTOHelp.getCacheDataArray(context);
		}
		//  授予
		if (source.hasOwnProperty('grants')) {
			_data.grants = source.grants;
		}
		//  grants
		if (source.hasOwnProperty('grants')) {
			_data.grants = source.grants;
		} else if (Auth_granted_systemDTOHelp.getEnableCache()) {
			_data.grants = await Auth_granted_systemDTOHelp.getCacheDataArray(
				context
			);
		}
		//  人员编号
		if (source.hasOwnProperty('employee_number')) {
			_data.employee_number = source.employee_number;
		}
		//  类型
		if (source.hasOwnProperty('employee_type')) {
			_data.employee_type = source.employee_type;
		}
		//  职位
		if (source.hasOwnProperty('title')) {
			_data.title = source.title;
		}
		//  联系电话
		if (source.hasOwnProperty('telephone_number')) {
			_data.telephone_number = source.telephone_number;
		}
		//  证件号
		if (source.hasOwnProperty('identification_number')) {
			_data.identification_number = source.identification_number;
		}
		//  地址
		if (source.hasOwnProperty('postal_address')) {
			_data.postal_address = source.postal_address;
		}
		// 附加额外数据
		if (source && Object.keys(source).length > 0 && keys) {
			Object.keys(source).forEach((key: string) => {
				if (!keys.includes(key) && !excludes.includes(key)) {
					_data[key] = source[key];
				}
			});
		}
		return _data;
	}

	/**
	 * 转化数组(数据对象转化成DTO数组)
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam[]} data 数据对象
	 * @return {*}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async toDtoArray(context: IContext, data: IParam[]) {
		const _data: any[] = [];
		if (data && Array.isArray(data) && data.length > 0) {
			for (let i = 0; i < data.length; i++) {
				const tempdata = await this.toDto(context, data[i]);
				_data.push(tempdata);
			}
		}
		return _data;
	}

	/**
	 * 处理响应DTO对象
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据对象
	 * @return {*}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async set(context: IContext, data: IParam) {
		const _data: IParam = await this.toDataObj(context, data);
		return _data;
	}

	/**
	 * 处理请求数据对象
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据对象
	 * @return {*}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async get(context: IContext, data: IParam = {}) {
		return await this.toDto(context, data);
	}

	/**
	 * 获取缓存数据
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {string} srfkey 数据主键
	 * @return {*}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async getCacheData(context: IContext, srfkey: string) {
		return {};
	}

	/**
	 * 获取缓存数组
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @return {*}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async getCacheDataArray(context: IContext) {
		return [];
	}

	/**
	 * 设置缓存数据
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据
	 * @return {*}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async setCacheData(context: IContext, data: IParam) {}

	/**
	 * 设置缓存数组
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {any[]} data 数据
	 * @return {*}
	 * @memberof Auth_infoDTOHelp
	 */
	public static async setCacheDataArray(context: IContext, data: any[]) {}
}
